﻿using Microsoft.Extensions.Configuration;
using System.Configuration;

namespace Utilities
{
    public static class ConfigurationHelper
    {
        /// <summary>
        /// 读取appsettings.json文件中的键值对的值
        /// </summary>
        /// <param name="value">键值</param>
        /// <param name="path">配置文件路径</param>
        /// <returns></returns>
        public static string GetConnectionString(string value, string path)
        {
            return JsonConfigurationExtensions.AddJsonFile(
                FileConfigurationExtensions.SetBasePath((IConfigurationBuilder)new Microsoft.Extensions.Configuration.ConfigurationBuilder(), Directory.GetCurrentDirectory()
                ), path).Build()[value];
        }
        /// <summary>
        /// 读取AppSettings.config文件中的键值对的值
        /// </summary>
        /// <param name="key">键值</param>
        /// <returns></returns>
        public static string GetConfigValue(string key)
        {
           Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            return configuration.AppSettings.Settings[key] != null ? configuration.AppSettings.Settings[key].Value : string.Empty;
        }
    }
}
